﻿import mx.utils.Delegate;
class cn.eminia.AS2.BoGV2 {
	// [用法]
	//var bs=new cn.eminia.AS2.BoGV2();
	//...for循环做按钮...
	//bs.push(oo);
	//}
	//bs.set(0) ;
	// 
	var _g:Array = [];
	var _id:Number;
	function BoGV2() {
	}
	function push(btn) {
		this._g.push(btn);
		btn.gid = this._g.length-1;
		btn._onRelease3 = btn.onRelease;
		btn.gp = this;
		btn.onRelease = function() {
			// trace('gid '+this.gid+'\t'+this.gp._id);
			trace(btn.cb+' '+btn.cb()+' || '+this.gid+' '+this.gp._id);
			if (this.gid == this.gp._id || btn.cb() ) {
				return;
			}
			// get ID          
			//
			this._onRelease3({gid:this.gid, id:getLastID(this._name)});
			var prev = this.gp._g[this.gp._id];
			//trace('prev -> '+prev+'.onRollOut -> '+prev.onRollOut );
			//trace('prev -> '+prev+'._onRollOut -> '+prev._onRollOut );
			//trace('prev -> '+prev+'._onRollOut2 -> '+prev._onRollOut2);
			prev._onRollOut2();
			BoGV2.reback(prev);
			this.gp.set(this.gid);
		};
	}
	function set(id) {
		var btn = this._g[id];
		btn.onRollOver();
		this._id = id;
		record(btn);
		del(btn);
	}
	function getID() {
		return this._id;
	}
	function get id() {
		return this._id;
	}
	static function getLastID(nm:String) {
		var a = nm.split('');
		for (var i = a.length; i>=0; i--) {
			if (!isNaN(a[i])) {
				return Number(nm.substring(i, a.length));
			}
		}
		return null;
	}
	static function record(btn) {
		btn._onRollOut2 = btn.onRollOut;
		btn._onRollOver2 = btn.onRollOver;
		btn._onRelease2 = btn.onRelease;
		btn._onReleaseOutside2 = btn.onReleaseOutside;
		btn._onPress2 = btn.onPress;
	}
	static function del(btn) {
		delete btn.onRollOut;
		delete btn.onRollOver;
		delete btn.onRelease;
		delete btn.onReleaseOutside;
		delete btn.onPress;
	}
	static function reback(btn) {
		btn.onRollOut = btn._onRollOut2;
		btn.onRollOver = btn._onRollOver2;
		btn.onRelease = btn._onRelease2;
		btn.onReleaseOutside = btn._onReleaseOutside2;
		btn.onPress = btn._onPress2;
		delete btn._onRollOut2;
		delete btn._onRollOver2;
		delete btn._onRelease2;
		delete btn._onReleaseOutside2;
		delete btn._onPress2;
	}
}
